<?php

namespace app\models;

use app\models\Personinfo;
use yii\base\Model;

/**
 * LoginForm is the model behind the login form.
 *
 * @property User|null $user This property is read-only.
 *
 */
class LoginForm extends Model
{
    public $stu_id;
    public $password; 
    public $message;
    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            
            [['stu_id', 'password'], 'required'], 
        	[['stu_id'], 'integer']
        ];
    }

    
    public function login(){
 
    	$personinfo=Personinfo::find()
   			 ->where(['stu_id' => $this->stu_id])
    		 ->one();
    	if($personinfo!=NULL)
    	{
    		$hash=$personinfo->password_hash;
    		if(\Yii::$app->getSecurity()->validatePassword($this->password, $hash))
    		{   //将学生信息存入session
    			$stu=[
    				'stuname'=>$personinfo->stu_name,
    				'stuid'=>$personinfo->stu_id,
    				
	  			];   			
    			$session=\Yii::$app->getSession();
    			$session->set('stu', $stu);
    			return true;				    		
    		}
    		else 
    		{
    			$this->message='密码错误！';
    			return false;   
    		}
    	}else 
    	{    
    		$this->message='该用户不存在！';
    		return false;
    	}
    	
    	
    	
    }
    
    
    public function attributeLabels()
    {
        return [
            'stu_id' => '学号',
            'password'=>'密码',
        ];
    }
    
}
